// 商城订单
import { get, post } from '@common/ajax';
import logger from '@common/logger';
import { TipSuccess } from '@common/tip';
import { SHOP_URL, INVENTORY_URL } from '@common/url';
import {
    SET_SHOP_ORDER_LIST_FILTER,
    GET_SHOP_ORDER_LIST,
    SET_AFTER_SALE_ORDER_LIST_FILTER,
    GET_AFTER_SALE_ORDER_LIST,
    GET_SHOP_ORDER_DETAIL,
    SET_AFTER_SALE_GOODS_INFO,
    SET_AFTER_SALE_ORDER_INFO,
    SET_AFTER_SALE_TYPE,
    CHANGE_AFTER_SALE_REMARK,
    GET_AFTER_SALE_ORDER_DETAIL,
    GET_AVALIABLE_EXPRESS_LIST,
} from '@store/mutationType';
const shopOrder = {
    state: {
        orderList: [],
        orderDetail: {},
        // 订单列表查询相关
        categoryValue: '',
        queryValue: '',
        pageNumberValue: 1,
        pageSizeValue: 10,
        // 售后相关
        afterSaleOrderList: [],
        afterSaleOrderDetail: {},
        afterSaleGoods: null,
        afterSaleOrderInfo: null,
        afterSaleType: 1,
        afterPageNumber: 1,
        afterPageSize: 10,
        // 可选快递列表
        expressList: [],
    },
    mutations: {
        SET_SHOP_ORDER_LIST_FILTER: (state, { categoryValue, queryValue='', pageNumberValue=1, pageSizeValue=10 }) => {
            Object.assign(state, {
                categoryValue,
                queryValue,
                pageNumberValue,
                pageSizeValue,
            });
        },
        GET_SHOP_ORDER_LIST: (state, orderList) => {
            Object.assign(state, {
                orderList,
            });
        },
        GET_SHOP_ORDER_DETAIL: (state, orderDetail) => {
            Object.assign(state, {
                orderDetail,
            });
        },
        // SET_AFTER_SALE_GOODS_INFO: (state, afterSaleGoods) => {
        //     Object.assign(state, {
        //         afterSaleGoods,
        //     });
        // },
        // SET_AFTER_SALE_ORDER_INFO: (state, afterSaleOrderInfo) => {
        //     Object.assign(state, {
        //         afterSaleOrderInfo,
        //     });
        // },
        // SET_AFTER_SALE_TYPE: (state, afterSaleType) => {
        //     Object.assign(state, {
        //         afterSaleType,
        //     });
        // },
        SET_AFTER_SALE_ORDER_LIST_FILTER: (state, { afterPageNumber, afterPageSize }) => {
            Object.assign(state, {
                afterPageNumber,
                afterPageSize,
            });
        },
        GET_AFTER_SALE_ORDER_LIST: (state, afterSaleOrderList) => {
            Object.assign(state, { afterSaleOrderList });
        },
        GET_AFTER_SALE_ORDER_DETAIL: (state, afterSaleOrderDetail) => {
            Object.assign(state, {
                afterSaleOrderDetail,
            });
        },
        GET_AVALIABLE_EXPRESS_LIST: (state, expressList) => {
            Object.assign(state, { expressList });
        },
    },
    actions: {
        // 查询组合单摘要
        getUniteOrderAbstract: async ({ commit }, requestId) => {
            const res = await get(SHOP_URL.orderPrepay, {
                request_id: requestId,
            });
            logger.success('获取的摘要信息为：', res);
            return res;
        },
        // 重新唤起支付
        repayUniteOrderAction: async ({ commit }, param) => {
            const res = await post(SHOP_URL.orderPay, param);
            logger.success('ShopOrder => 唤起支付结果', res);
            return res;
        },
        // 取消订单(订单明细中)
        cancelUniteOrderInDetail: async ({ dispatch, state }, {requestId, orderNumber}) => {
            const { categoryValue } = state;

            const res = await post(SHOP_URL.orderCancel, { request_id: requestId });
            logger.success('取消组合单（含支付信息）结果 => ', res);
            if (res) {
                dispatch('getShopOrderList', {
                    category: categoryValue,
                });
                dispatch('getOrderDetail', orderNumber);
                return true;
            }
            return false;
        },
        // 取消订单(列表中)
        cancelUniteOrderInList: async ({ dispatch, state }, requestId) => {
            const { categoryId } = state;
            const res = await post(SHOP_URL.orderCancel, { request_id: requestId });
            logger.success('取消组合单(列表中)（含支付信息）结果 => ', res);
            if (res) {
                dispatch('getShopOrderList', { categoryId });
                return true;
            }
            return false;
        },
        // // 直接取消订单
        // directCancelUniteOrder: async ({ dispatch, state }, requestId) => {
        //     const { categoryValue } = state;
        //     const res = await post(urls.shopUniteOrderDirectCancel, { request_id: requestId });
        //     logger.success('直接取消组合单结果 => ', res);
        //     if (res) {
        //         dispatch('getShopOrderList', {
        //             category: categoryValue,
        //         });
        //         return true;
        //     }
        //     return false;
        // },
        // 获取订单列表
        getShopOrderList: async ({ commit }, { category, query='' }) => {
            const list = await get(SHOP_URL.orderList, {
                category,
                query,
                page_no: 1,
                page_size: 10,
            });
            logger.success('ShopOrderList => list data', list.orders);
            commit(SET_SHOP_ORDER_LIST_FILTER, {
                categoryValue: category,
                queryValue: query,
                pageNumberValue: 1,
                pageSizeValue: 10,
            });
            commit(GET_SHOP_ORDER_LIST, list.orders);
        },
        // 获取下一页数据
        getShopOrderListNext: async ({ commit, state }) => {
            const { categoryValue, queryValue, pageNumberValue, pageSizeValue, orderList } = state;
            const list = await get(SHOP_URL.orderList, {
                category: categoryValue,
                query: queryValue,
                page_no: pageNumberValue + 1,
                page_size: pageSizeValue,
            });
            commit(SET_SHOP_ORDER_LIST_FILTER, {
                categoryValue,
                queryValue,
                pageNumberValue: pageNumberValue + 1,
                pageSizeValue,
            });
            const _list = [...orderList, ...list.orders];
            commit(GET_SHOP_ORDER_LIST, _list);
        },
        // 获取售后列表
        getAfterSaleOrderList: async ({ commit }) => {
            const page_no = 1;
            const page_size = 10;
            const res = await get(SHOP_URL.refundList, {
                page_no,
                page_size,
            });
            logger.success('ShopOrderList => AfterSale', res.applies);
            commit(SET_AFTER_SALE_ORDER_LIST_FILTER, {
                afterPageNumber: page_no,
                afterPageSize: page_size,
            });
            commit(GET_AFTER_SALE_ORDER_LIST, res.applies);
        },
        // 获取售后列表下一页
        getAfterSaleOrderListNext: async ({ commit, state }) => {
            const { afterPageNumber, afterPageSize, afterSaleOrderList } = state;
            const pageNumber = afterPageNumber + 1;
            const res = await get(SHOP_URL.refundList, {
                page_no: pageNumber,
                page_size: afterPageSize,
            });
            commit(SET_AFTER_SALE_ORDER_LIST_FILTER, {
                afterPageNumber: pageNumber,
                afterPageSize,
            });
            const list = [...afterSaleOrderList, ...res.applies];
            commit(GET_AFTER_SALE_ORDER_LIST, list);
        },
        // 获取订单详情
        getShopOrderDetail: async ({ commit }, orderNumber) => {
            const res = await get(SHOP_URL.orderInfo, {
                order_number: orderNumber,
            });
            logger.success('获取到的订单数据为：', res);
            commit(GET_SHOP_ORDER_DETAIL, res);
        },
        // // 确认收货
        // confirmShopOrder: async ({ dispatch }, orderNumber) => {
        //     const res = await post(SHOP_URL.orderConfirm, {
        //         order_number: orderNumber,
        //     });
        //     dispatch('getShopOrderDetail', orderNumber);
        //     return res ? true : false;
        // },
        // // 设置售后商品信息
        // setAfterSaleGoodsInfo: ({ commit }, info) => {
        //     logger.info('AfterSale => 设置商品信息', info);
        //     commit(SET_AFTER_SALE_GOODS_INFO, info);
        // },
        // // 设置整单售后信息
        // setAfterSaleOrderInfo: ({ commit }, orderInfo) => {
        //     logger.info('AfterSale => 设置售后订单信息', orderInfo);
        //     commit(SET_AFTER_SALE_ORDER_INFO, orderInfo);
        // },
        // // 设置售后类型
        // setAfterSaleType: ({ commit }, type) => {
        //     commit(SET_AFTER_SALE_TYPE, type);
        // },
        // // 售后申请
        // applyAfterSaleService: async ({ commit }, params) => {
        //     const res = await post(SHOP_URL.refundApply, params);
        //     return res ? res.apply_number : false;
        // },
        // // 获取售后详情
        // getAfterSaleOrderDetail: async ({ commit }, apply_number) => {
        //     const res = await get(SHOP_URL.refundDetail, { apply_number });
        //     logger.success('获取的售后详情 => ', res);
        //     commit(GET_AFTER_SALE_ORDER_DETAIL, res);
        // },
        // // 提交快递信息
        // addAfterSaleOrderExpressInfo: async ({ commit }, params) => {
        //     const res = await post(SHOP_URL.refundDeliver, params);
        //     if (res) {
        //         TipSuccess('添加物流信息成功！');
        //     }
        // },
        // // 取消退货、退款申请
        // cancelAfterSaleOrder: async ({ commit }, applyNumber) => {
        //     const res = await post(SHOP_URL.refundCancel, {
        //         apply_number: applyNumber,
        //     });
        //     if (res) {
        //         TipSuccess('取消成功！');
        //     }
        // },
        // 虚拟订单通知查询
        virtualOrderAction: async ({ dispatch }, payOrderNumber) => {
            // await get(`${urls.virtual}/${payOrderNumber}`, {
            //     from_success: 1,
            // });
        },
        // // 获取可用配送平台
        // getAvailableExpressList: async ({ commit }) => {
        //     const res = await get(INVENTORY_URL.express, {
        //         deliver_type: 2,
        //     });
        //     if (res) {
        //         commit(GET_AVALIABLE_EXPRESS_LIST, res.platforms);
        //     }
        // },
    },
};

export default shopOrder;
